/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.una.pol.ii.is2.controller.reportes;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.Option;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.event.ValueChangeEvent;
import py.una.pol.ii.is2.controller.ControlAcceso;
import py.una.pol.ii.is2.controller.ApplicationBean1;
import py.una.pol.ii.is2.controller.SessionBean1;
import py.una.pol.ii.is2.controller.RequestBean1;
import py.una.pol.ii.is2.extra.Messages;
import py.una.pol.ii.is2.reporter.SAVReporter;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version ReportesVehiculos.java
 * @version Created on Jul 25, 2009, 2:43:39 AM
 * @author markos
 */
public class ReportesVehiculos extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private HtmlPanelGrid gridPanelMarca = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelMarca() {
        return gridPanelMarca;
    }

    public void setGridPanelMarca(HtmlPanelGrid hpg) {
        this.gridPanelMarca = hpg;
    }
    private DropDown dropDownTipoInforme = new DropDown();

    public DropDown getDropDownTipoInforme() {
        return dropDownTipoInforme;
    }

    public void setDropDownTipoInforme(DropDown dd) {
        this.dropDownTipoInforme = dd;
    }
    private TextField txtCodMarca = new TextField();

    public TextField getTxtCodMarca() {
        return txtCodMarca;
    }

    public void setTxtCodMarca(TextField tf) {
        this.txtCodMarca = tf;
    }
    private HtmlPanelGrid gridPanelModelo = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelModelo() {
        return gridPanelModelo;
    }

    public void setGridPanelModelo(HtmlPanelGrid hpg) {
        this.gridPanelModelo = hpg;
    }
    private HtmlPanelGrid gridPanelTipo = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelTipo() {
        return gridPanelTipo;
    }

    public void setGridPanelTipo(HtmlPanelGrid hpg) {
        this.gridPanelTipo = hpg;
    }
    private HtmlPanelGrid gridPanelUbicacion = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelUbicacion() {
        return gridPanelUbicacion;
    }

    public void setGridPanelUbicacion(HtmlPanelGrid hpg) {
        this.gridPanelUbicacion = hpg;
    }
    private TextField txtCodModelo = new TextField();

    public TextField getTxtCodModelo() {
        return txtCodModelo;
    }

    public void setTxtCodModelo(TextField tf) {
        this.txtCodModelo = tf;
    }
    private TextField txtCodTipo = new TextField();

    public TextField getTxtCodTipo() {
        return txtCodTipo;
    }

    public void setTxtCodTipo(TextField tf) {
        this.txtCodTipo = tf;
    }
    private DropDown dropDownUbicacion = new DropDown();

    public DropDown getDropDownUbicacion() {
        return dropDownUbicacion;
    }

    public void setDropDownUbicacion(DropDown dd) {
        this.dropDownUbicacion = dd;
    }
    private TextField txtKmInicial = new TextField();

    public TextField getTxtKmInicial() {
        return txtKmInicial;
    }

    public void setTxtKmInicial(TextField tf) {
        this.txtKmInicial = tf;
    }
    private TextField txtKmFinal = new TextField();

    public TextField getTxtKmFinal() {
        return txtKmFinal;
    }

    public void setTxtKmFinal(TextField tf) {
        this.txtKmFinal = tf;
    }
    private TextField txtNroInicial = new TextField();

    public TextField getTxtNroInicial() {
        return txtNroInicial;
    }

    public void setTxtNroInicial(TextField tf) {
        this.txtNroInicial = tf;
    }
    private TextField txtNroFinal = new TextField();

    public TextField getTxtNroFinal() {
        return txtNroFinal;
    }

    public void setTxtNroFinal(TextField tf) {
        this.txtNroFinal = tf;
    }
    private HtmlPanelGrid gridPanelKm = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelKm() {
        return gridPanelKm;
    }

    public void setGridPanelKm(HtmlPanelGrid hpg) {
        this.gridPanelKm = hpg;
    }
    private HtmlPanelGrid gridPanelNumeracion = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelNumeracion() {
        return gridPanelNumeracion;
    }

    public void setGridPanelNumeracion(HtmlPanelGrid hpg) {
        this.gridPanelNumeracion = hpg;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public ReportesVehiculos() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("ReportesVehiculos Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        cargaOpciones();
        cargaOpcionesUbicacion();
        desactivaPanels();
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ControlAcceso getControlAcceso() {
        return (ControlAcceso) getBean("ControlAcceso");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    public String btnReporte_action() {

        try {
            Map params = new HashMap();
            String nombreReporte = "";
            if (dropDownTipoInforme.getSelected().toString().equals("marca")) {
                params.put("marca", Integer.parseInt(txtCodMarca.getText().toString()));
                nombreReporte = "vehiculo-marca.jasper";
            } else if (dropDownTipoInforme.getSelected().toString().equals("tipo")) {
                params.put("tipo", Integer.parseInt(txtCodTipo.getText().toString()));
                nombreReporte = "vehiculo-tipo.jasper";
            } else if (dropDownTipoInforme.getSelected().toString().equals("modelo")) {
                params.put("modelo", Integer.parseInt(txtCodModelo.getText().toString()));
                nombreReporte = "vehiculo-modelo.jasper";
            } else if (dropDownTipoInforme.getSelected().toString().equals("kilometraje")) {
                params.put("kilometrajeInicio", Integer.parseInt(txtKmInicial.getText().toString()));
                params.put("kilometrajeFin", Integer.parseInt(txtKmFinal.getText().toString()));
                nombreReporte = "vehiculo-kilometraje.jasper";
            } else if (dropDownTipoInforme.getSelected().toString().equals("ubicacion")) {
                params.put("ubicacion", Integer.parseInt(dropDownUbicacion.getSelected().toString()));
                nombreReporte = "vehiculo-ubicacion.jasper";
            } else if (dropDownTipoInforme.getSelected().toString().equals("numeracion")) {
                params.put("numeracionInicio", Integer.parseInt(txtNroInicial.getText().toString()));
                params.put("numeracionFin", Integer.parseInt(txtNroFinal.getText().toString()));
                nombreReporte = "vehiculo-numeracion.jasper";
            }
            // genera el reporte
            new SAVReporter().getReport(nombreReporte, params, null, nombreReporte);
        } catch (Exception e) {
            error(Messages.PROBLEMAS_REPORTE);
            e.printStackTrace();
        }

        return null;
    }
    private List<Option> opcionesReporte;
    private List<Option> opcionesUbicacion;

    public List<Option> getOpcionesUbicacion() {
        return opcionesUbicacion;
    }

    public void setOpcionesUbicacion(List<Option> opcionesUbicacion) {
        this.opcionesUbicacion = opcionesUbicacion;
    }

    public List<Option> getOpcionesReporte() {
        return opcionesReporte;
    }

    public void setOpcionesReporte(List<Option> opcionesReporte) {
        this.opcionesReporte = opcionesReporte;
    }

    private void cargaOpciones() {
        opcionesReporte = new ArrayList<Option>();
        opcionesReporte.add(new Option("0", "--- Elija Una Opcion ---"));
        opcionesReporte.add(new Option("tipo", "Tipo de vehículo"));
        opcionesReporte.add(new Option("marca", "Marca del vehículo"));
        opcionesReporte.add(new Option("modelo", "Modelo del vehículo"));
        opcionesReporte.add(new Option("numeracion", "Rango de numeración"));
        opcionesReporte.add(new Option("ubicacion", "Ubicación del vehículo"));
        opcionesReporte.add(new Option("kilometraje", "Rangos de kilometraje"));
    }

    private void cargaOpcionesUbicacion() {
        opcionesUbicacion = new ArrayList<Option>();
        opcionesUbicacion.add(new Option("0", "En servicio"));
        opcionesUbicacion.add(new Option("1", "En taller"));
        opcionesUbicacion.add(new Option("2", "En corral"));
    }
    
    public void dropDownTipoInforme_processValueChange(ValueChangeEvent event) {
        desactivaPanels();
        if (dropDownTipoInforme.getSelected().toString().equals("marca")) {
            gridPanelMarca.setRendered(true);
        } else if (dropDownTipoInforme.getSelected().toString().equals("tipo")) {
            gridPanelTipo.setRendered(true);
        } else if (dropDownTipoInforme.getSelected().toString().equals("modelo")) {
            gridPanelModelo.setRendered(true);
        } else if (dropDownTipoInforme.getSelected().toString().equals("kilometraje")) {
            gridPanelKm.setRendered(true);
        } else if (dropDownTipoInforme.getSelected().toString().equals("ubicacion")) {
            gridPanelUbicacion.setRendered(true);
        } else if (dropDownTipoInforme.getSelected().toString().equals("numeracion")) {
            gridPanelNumeracion.setRendered(true);
        }
    }

    private void desactivaPanels() {
        gridPanelMarca.setRendered(false);
        gridPanelModelo.setRendered(false);
        gridPanelTipo.setRendered(false);
        gridPanelUbicacion.setRendered(false);
        gridPanelNumeracion.setRendered(false);
        gridPanelUbicacion.setRendered(false);
        gridPanelKm.setRendered(false);
    }
}


